package com.lab.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lab.dao.CollegeDao;
import com.lab.dao.CourseDao;
import com.lab.dao.MajorDao;
import com.lab.dao.PassWordDao;
import com.lab.dao.ScoreDao;
import com.lab.dao.StudentDao;
import com.lab.dao.impl.CollegeDaoImpl;
import com.lab.dao.impl.CourseDaoImpl;
import com.lab.dao.impl.MajorDaoImpl;
import com.lab.dao.impl.PassWordDaoImpl;
import com.lab.dao.impl.ScoreDaoImpl;
import com.lab.dao.impl.StudentDaoImpl;

//控制层-删除业务逻辑处理 
@WebServlet("/deleteServlet")
public class DeleteServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = (String) request.getParameter("method");
		if ("deleteStudent".equals(method)) {
			deleteStudent(request, response);
		}else if ("deleteCollege".equals(method)) {
			deleteCollege(request, response);
		}else if("deleteMajor".equals(method)){
			deleteMajor(request, response);
		}else if("deleteCourse".equals(method)){
			deleteCourse(request, response);
		}

	}

	private void deleteCourse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int cou_id=Integer.parseInt(request.getParameter("cou_id"));
		CourseDao cd=new CourseDaoImpl();
		int i=cd.deleteCourse(cou_id);
		if(i!=0){
			System.out.println("删除成功!!");
			request.getRequestDispatcher("showAllServlet?method=showCourse").forward(request, response);
		}else{
			 System.out.println("删除失败!!");
			 request.getRequestDispatcher("showAllServlet?method=showCourse").forward(request, response);
		}
	}

	private void deleteStudent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		StudentDao studao = new StudentDaoImpl();
		
		int id=Integer.parseInt(request.getParameter("id"));
		boolean flag=studao.delStudent(id);
		ScoreDao sd=new ScoreDaoImpl();
		sd.deleteScore(id);
		PassWordDao pd=new PassWordDaoImpl();
		pd.deleteStuPwd(id);
		//如果删除成功，返回到displaystudent.jsp页面，如果不成功，给用户提示信息
		if(flag){
			System.out.println("删除成功!!");
			request.getRequestDispatcher("showAllServlet?method=showStudent").forward(request, response);
		}else{
			 System.out.println("删除失败!!");
			 request.getRequestDispatcher("showAllServlet?method=showStudent").forward(request, response);
		}
		
	}

	private void deleteCollege(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int col_id = Integer.parseInt(request.getParameter("col_id"));
		CollegeDao collegedao=new CollegeDaoImpl();
		int i=collegedao.deleteCollege(col_id);
		if(i!=0){
			System.out.println("删除成功!!");
			request.getRequestDispatcher("showAllServlet?method=showColMaj").forward(request, response);
		}else{
			System.out.println("删除失败!!");
			request.getRequestDispatcher("showAllServlet?method=showColMaj").forward(request, response);
		}
	}

	private void deleteMajor(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int maj_id=Integer.parseInt(request.getParameter("maj_id"));
		MajorDao majordao=new MajorDaoImpl();
		int i=majordao.deleteMajor(maj_id);
		if(i!=0){
			System.out.println("删除成功!!");
			request.getRequestDispatcher("showAllServlet?method=showColMaj").forward(request, response);
		}else{
			System.out.println("删除失败!!");
			request.getRequestDispatcher("showAllServlet?method=showColMaj").forward(request, response);
		}
	}

}
